package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.MenuDao;
import com.itheima.dao.PermissionDao;
import com.itheima.dao.RoleDao;
import com.itheima.entity.PageResult;
import com.itheima.pojo.Permission;
import com.itheima.pojo.Role;
import com.itheima.service.PermissionService;
import com.itheima.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service(interfaceClass = RoleService.class)
@Transactional
public class RoleServiceImpl implements RoleService {

    @Autowired
    private RoleDao roleDao;
    @Autowired
    private MenuDao menuDao;
    @Autowired
    private PermissionDao permissionDao;



    @Override
    public List<Role> findAll() {
        return roleDao.findAll();
    }
    //分页查询

    @Override
    public PageResult findPage(Integer pageNum, Integer sizeNum, String queryString) {
        PageHelper.startPage(pageNum,sizeNum);
        Page<Role> page = roleDao.findPage(queryString);

        return new PageResult(page.getTotal(),page.getResult());
    }


    //添加角色及其关联
    @Override
    public void addRole(Role role, Integer[] menuIds, Integer[] permissionIds) {
        roleDao.add(role);
        Integer roleId = role.getId();
        if(menuIds !=null && menuIds.length>0){
            for (Integer menuId : menuIds) {
                Map<String,Integer> map= new HashMap<>();
                map.put("roleId",roleId);
                map.put("menuId",menuId);
                menuDao.addMenuAndRole(map);
            }
        }

        if(permissionIds !=null && permissionIds.length>0){
            for (Integer permissionId : permissionIds) {
                Map<String,Integer> map= new HashMap<>();
                map.put("roleId",roleId);
                map.put("permissionId",permissionId);
                permissionDao.addPerAndRole(map);
            }
        }
        
        
    }
    //回显角色信息
    @Override
    public Role getRole(Integer id) {
        return roleDao.findById(id);
    }
    //编辑修改角色信息
    @Override
    public void updateRole(Role role, Integer[] menuIds, Integer[] permissionIds) {

        roleDao.deleteMenuWithRoleId(role.getId());
        roleDao.deletePermissionWithRoleId(role.getId());
        roleDao.updateRole(role);
        Integer roleId = role.getId();
        if(menuIds !=null && menuIds.length>0){
            for (Integer menuId : menuIds) {
                Map<String,Integer> map= new HashMap<>();
                map.put("roleId",roleId);
                map.put("menuId",menuId);
                menuDao.addMenuAndRole(map);
            }
        }

        if(permissionIds !=null && permissionIds.length>0){
            for (Integer permissionId : permissionIds) {
                Map<String,Integer> map= new HashMap<>();
                map.put("roleId",roleId);
                map.put("permissionId",permissionId);
                permissionDao.addPerAndRole(map);
            }
        }
    }


    //删除角色
    @Override
    public void deleteRole(Integer id) {
        roleDao.deletePermissionWithRoleId(id);
        roleDao.deleteMenuWithRoleId(id);
        roleDao.delete(id);
    }



}
